package se.energimyndigheten.lampguiden.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.webkit.WebView;

import java.io.IOException;

/*
This web view will replace certain expressions and symbols if it's being accessed by a screen reader.
 */
public class AccessibleEnhancedWebView extends WebView {
    Context context;
    public AccessibleEnhancedWebView(Context context) {
        super(context);
        this.context = context;
    }
    
    public AccessibleEnhancedWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }
    
    
    public AccessibleEnhancedWebView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
    }

    @SuppressLint("NewApi")
    public AccessibleEnhancedWebView(Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) {
        super(context, attrs, defStyle, privateBrowsing);
        this.context = context;
    }

    /*
    Replace certain symbols, phrases and write out entity shortcuts.
    FIXME needs to be translated if apps start supporting more languages
    */
    private String fiddleText(String data) {
        data = data.replace("&nbsp;"," ");
        data = data.replace("lm", "lumen");
        /*
        data = data.replace("ä", "e");
        data = data.replace("Ä", "E");
        data = data.replace("å", "a");
        data = data.replace("Å", "A");
        data = data.replace("ö", "e"); // "&#xf6;");
        data = data.replace("Ö", "E");
        */
        data = data.replace("&amp;", "och");
        return data;
    }
    
    @Override
    public void loadUrl(String url) {
        String prefix = "file:///android_asset/";
        if (Compat.getInstance().isAccessibilityEnabled(context) &&
            url.startsWith(prefix)) {
            try {
                Compat.getInstance().enableJavaScript(this);
                String html = AssetsUtil.getFileContents(context.getAssets(), url.substring(prefix.length()));
                html = fiddleText(html);
                super.loadDataWithBaseURL("file:///android_asset/" + AssetsUtil.HTML_PATH_CONVERSION_TABLE, html, "text/html", "UTF-8",null);
            } catch (IOException e) {
                super.loadUrl(url);
            }
        } else {
            super.loadUrl(url);
        }
    }

    @Override
    public void loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl) {
        if (Compat.getInstance().isAccessibilityEnabled(context)) {
            Compat.getInstance().enableJavaScript(this);
            data = fiddleText(data);
            super.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl);
        } else{
            super.loadDataWithBaseURL(baseUrl, data, mimeType, encoding,historyUrl);
        }
    }
}
